package handler

import (
	"github.com/gin-gonic/gin"
	"mime/multipart"
	"trpg-base/dto"
	"trpg-base/entity"
	"trpg-common/request"
	"trpg-common/response"
	"trpg-common/utils/gorm_util"
)

type RoomHandler struct{}

func (RoomHandler) AddRoom(c *gin.Context) {
	room := entity.Room{}
	request.GetStructFromGinContext(c, &room)
	cover, err := c.FormFile("cover")
	if err != nil {
		cover = nil
	}
	user := request.GetUserFromGinContext(c)
	if room.Id == 0 {
		response.SuccessWithData(c, roomService.AddRoom(room, cover, user))
	} else {
		response.SuccessWithData(c, roomService.UpdateRoom(room, cover, user))
	}

}

func (RoomHandler) SearchRoomList(c *gin.Context) {
	roomRequest := dto.RoomRequest{}
	request.GetStructFromGinContext(c, &roomRequest)
	pageRequest := gorm_util.PageRequest{}
	pageRequest.Page = int32(request.GetIntFromQuery(c, "page"))
	pageRequest.PageSize = int32(request.GetIntFromQuery(c, "pageSize"))
	user := request.GetUserFromGinContext(c)
	response.SuccessWithData(c, roomService.SearchRooms(roomRequest, pageRequest, user))
}

func (RoomHandler) GetRoomById(c *gin.Context) {
	id := request.GetIntFromPath(c, "id")
	user := request.GetUserFromGinContext(c)
	response.SuccessWithData(c, roomService.GetRoomDetail(id, user))
}

func (RoomHandler) GetMembers(c *gin.Context) {
	id := request.GetIntFromPath(c, "id")
	response.SuccessWithData(c, roomService.GetMembers(id))
}

func (RoomHandler) BindCharacter(c *gin.Context) {
	roomId := request.GetIntFromPath(c, "roomId")
	characterId := request.GetIntFromPath(c, "characterId")
	user := request.GetUserFromGinContext(c)
	roomService.BindCharacter(roomId, characterId, user)
	response.Success(c)
}

func (RoomHandler) BindScript(c *gin.Context) {
	roomId := request.GetIntFromPath(c, "roomId")
	scriptId := request.GetIntFromPath(c, "scriptId")
	user := request.GetUserFromGinContext(c)
	roomService.BindScript(roomId, scriptId, user)
	response.Success(c)
}

func (RoomHandler) ApplyJoinRoom(c *gin.Context) {
	room := entity.Room{}
	request.GetStructFromGinContext(c, &room)
	user := request.GetUserFromGinContext(c)
	roomService.ApplyJoinRoom(room, user)
	response.Success(c)
}

func (RoomHandler) DealWithApply(c *gin.Context) {
	uid := request.GetStringFromPath(c, "uid")
	flag := request.GetBoolFromPath(c, "flag")
	room := entity.Room{}
	request.GetStructFromGinContext(c, &room)
	user := request.GetUserFromGinContext(c)
	roomService.DealApply(room, uid, flag, user)
	response.Success(c)
}

func (RoomHandler) DeleteMember(c *gin.Context) {
	room := entity.Room{}
	request.GetStructFromGinContext(c, &room)
	uid := request.GetStringFromPath(c, "uid")
	username := request.GetStringFromPath(c, "username")
	user := request.GetUserFromGinContext(c)
	roomService.DeleteMembers(room, uid, username, user)
	response.Success(c)
}

func (RoomHandler) QuitRoom(c *gin.Context) {
	room := entity.Room{}
	request.GetStructFromGinContext(c, &room)
	user := request.GetUserFromGinContext(c)
	roomService.QuitRoom(room, user)
	response.Success(c)
}

func (RoomHandler) SendMessageFiles(c *gin.Context) {
	vo := struct {
		RoomId int64                 `json:"roomId" form:"roomId"`
		File   *multipart.FileHeader `json:"file" form:"file"`
	}{}
	request.GetStructFromGinContext(c, &vo)
	file, path := roomService.SendMessageFiles(vo.RoomId, vo.File)
	response.SuccessWithData(c, gin.H{
		"url":  file,
		"path": path,
	})
}

func (RoomHandler) GetRoomMessages(c *gin.Context) {
	roomId := request.GetIntFromPath(c, "roomId")
	response.SuccessWithData(c, roomService.GetRoomMessages(roomId))
}
